package app.netant.finishbook.uu234;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
/**
 * UU234明细处理
 * @author JiaJ
 *
 */
public class UU234Detail extends NetAntBase {
	
	private UU234Entity entity;

	public UU234Detail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(UU234Entity entity){
		this.entity = entity;
	}
	
	/**
	 * 查看类型
	 */
	public void analyseType(){		
		Elements activePart = document.select(".crumbswrap a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}	
	}
	
	/**
	 * 查看标题
	 */
	public void analyseTitle(){
		Elements activePart = document.select(".wrap980 .wrap706 .con_lwrap .r420 h1");
		if(activePart.size()>0){	
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);			
			this.entity.setNovelName(title);
		}
	}
	
	/**
	 * 解析标签
	 */
	public void analyseTag(){
		Elements activePart = document.select(".wrap980 .wrap706 .con_lwrap .r420 p.author");
		if(activePart.size()>0){	
			String tags = activePart.get(0).text();
			
			String[] tagm = tags.split("：");
			String keys = "";
			String vals = "";
			
			for(int i=0;i<tagm.length;i++){
				
				String tmp = tagm[i].trim().replaceAll(" MB", "MB");
				
				if(i==0){
					keys += "," + tmp.replaceAll(" ", "");
				}
				else if(i==tagm.length-1){
					vals += "," + tmp.replaceAll(" ", "");
				}
				else{
					
					int first = tmp.indexOf(" ");
					if(first>0){
						keys += "," + tmp.substring(first).replaceAll(" ", "");
						vals += "," + tmp.substring(0,first).replaceAll(" ", "");
					}
					
				}
				
			}
			
			if(!"".equals(keys)){
				keys = keys.substring(1);
				vals = vals.substring(1);
			}
			
			String[] keyS = keys.split(",");
			String[] valS = vals.split(",");
			
			for(int j=0;j<keyS.length;j++){
				
				System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
				
				this.entity.setTag(keyS[j], valS[j]);
				
			}	
		}
	}
	
	/**
	 * 简介分析
	 */
	public void analyseMemo(){
		
		Elements activePart = document.select(".wrap980 .wrap706 .con_lwrap .r420 .r_cons");
		if(activePart.size()>0){	
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	/**
	 * 分析下载
	 */
	public void analyseHref(){
		
		Elements activePart = document.select("#download .downmain .downmenu a");
		String href = "";
		if(activePart.size()>1){
			href = activePart.get(1).attr("href");
		}
		else if(activePart.size()>0){
			href = activePart.get(0).attr("href");
		}
		else{
			throw new RuntimeException("无法获取下载链接！");
		}
		
		System.out.println("【Href】:"+href);
		
		int index = href.lastIndexOf(".");
		String ext = "";
		if(index>0){
			ext = href.substring(index);
		}
		
		//补全名称
		String name = this.entity.getNovelName();
		this.entity.setNovelFullName(name+ext);
		
		this.entity.setDownPath(href);
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTag();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		UU234Detail detail = new UU234Detail("http://www.uu234.net/b3663/#download");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTag();
		detail.analyseMemo();
		detail.analyseHref();
		
		
	}
	
}
